package com.example.leepong.Game.Interaction;

import com.example.leepong.Game.Engine;
import com.example.leepong.Game.Shapes.*;

/**
 * Created by Ichigo on 21/04/2014.
 */
public class TouchManager {
    public float TOUCH_TOLERANCE = 0.2f;

    private Engine _engine;

    public TouchManager(Engine engine) {
        _engine = engine;
    }

    public boolean OnLeftRaquetTap(Coordinates tapCoords) {
        _engine.TopRacket.Player.TouchTap(tapCoords);
        return true;
    }

    public boolean OnRightRaquetTap(Coordinates tapCoords) {
        _engine.BottomRacket.Player.TouchTap(tapCoords);
        return true;
    }

    public boolean OnLandTap(Coordinates tapCoords) {

        if (this.getTopRacketTouchZone().Contains(tapCoords)) {
            _engine.TopRacket.Player.TouchZoneTap(tapCoords);
            return true;
        }

        if (this.getBottomRacketTouchZone().Contains(tapCoords)) {
            _engine.BottomRacket.Player.TouchZoneTap(tapCoords);
            return true;
        }

        _engine.PauseResume(true);
        return true;
    }

    public boolean OnLeftRaquetScroll(Coordinates newCoords, Coordinates oldCoords) {
        _engine.TopRacket.Player.TouchScroll(newCoords, oldCoords);
        return true;
    }

    public boolean OnRightRaquetScroll(Coordinates newCoords, Coordinates oldCoords) {
        _engine.BottomRacket.Player.TouchScroll(newCoords, oldCoords);
        return true;
    }

    public boolean OnLandScroll(Coordinates newCoords, Coordinates oldCoords) {
        if (this.getTopRacketTouchZone().Contains(oldCoords)) {
            _engine.TopRacket.Player.TouchZoneScroll(newCoords, oldCoords);
            return true;
        }

        if (this.getBottomRacketTouchZone().Contains(oldCoords)) {
            _engine.BottomRacket.Player.TouchZoneScroll(newCoords, oldCoords);
            return true;
        }

        return false;
    }

    private Rectangle _bottomRacketTouchZone;

    private Rectangle getBottomRacketTouchZone() {
        if (_bottomRacketTouchZone == null) {
            Rectangle r = _engine.BottomRacket.Rect;
            FixedRectangle lr = _engine.Land.Rect;

            int tolerance = (int) (TOUCH_TOLERANCE * r.Height);
            _bottomRacketTouchZone = new Rectangle(lr.Left, r.Top() - tolerance, lr.Width, r.Height + tolerance);
        }
        return _bottomRacketTouchZone;
    }

    private Rectangle _topRacketTouchZone;

    private Rectangle getTopRacketTouchZone() {
        if (_topRacketTouchZone == null) {
            Rectangle r = _engine.TopRacket.Rect;
            FixedRectangle lr = _engine.Land.Rect;
            int tolerance = (int) (TOUCH_TOLERANCE * r.Height);
            _topRacketTouchZone = new Rectangle(lr.Left, r.Top(), lr.Width, r.Height + tolerance);
        }
        return _topRacketTouchZone;
    }
}
